//
//  UserLevelController.m
//  CoolTennisBall
//
//  Created by Coollang on 16/9/13.
//  Copyright © 2016年 CoolLang. All rights reserved.
//

#import "UserLevelController.h"
#import "UserGroup.h"
#import "UserItem.h"
#import "UserSaveItem.h"
#import "UserCenterCell.h"

@interface UserLevelController ()
/**
 *  当前选中的cell
 */
@property (nonatomic, strong)UserCenterCell *currentCell;
@end

@implementation UserLevelController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self loadData];
    
}

- (void)loadData {
    UserGroup *group0 = [[UserGroup alloc] init];
    group0.headHeight = kAutoHei(24);
    
    for (NSString *levelStr in self.tennisLevels) {
        UserSaveItem *saveItem = [UserSaveItem itemWithTitle:levelStr];
        [group0.items addObject:saveItem];
    }
    
    [self.groups addObject:group0];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UserCenterCell *cell = [UserCenterCell settingViewCellTableView:tableView withStyle:UITableViewCellStyleValue1];
    
    UserGroup *group = self.groups[indexPath.section];
    UserSaveItem *item = group.items[indexPath.row];
    cell.item = item;
    
    if (self.currentCell == nil && [item.title isEqualToString:self.useSettingText]) {
        self.currentCell = cell;
        cell.selected = YES;
        [cell setSelected:YES animated:NO];
        
    }
    
    if (indexPath.row == group.items.count - 1) {
        [cell showLine:NO];
    }else {
        [cell showLine:YES];
    }
    if (indexPath.row == 0) {
        [cell showSectionSepTopLineView:YES];
    }else {
        [cell showSectionSepTopLineView:NO];
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    // 取出对应的模型数据
    UserGroup *group = self.groups[indexPath.section];
    UserItem *item = group.items[indexPath.row];
    
    // 2.判断有没有block,有的话就执行
    if (item.block) {
        item.block();
        return;
    }
    UserCenterCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    if (self.currentCell) {
        self.currentCell.selected = NO;
    }
    
    cell.selected = YES;
    self.currentCell = cell;
    
    if (self.userSelectLevelBlock) {
        self.userSelectLevelBlock(self.currentCell.item.title);
        [self.navigationController popViewControllerAnimated:YES];
    }
    
}
@end
